package br.com.ipcalc;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidarIp {

	public boolean validar(String ip) {

		String regex = "\\d?\\d?\\d[.]\\d?\\d?\\d[.]\\d?\\d?\\d[.]\\d?\\d?\\d";
		String[] ipArray = ip.split("[.]");
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(ip);
		if (matcher.matches()) {			
			if ((Integer.parseInt(ipArray[0]) <= 255 && Integer
					.parseInt(ipArray[0]) >= 0)
					&& (Integer.parseInt(ipArray[1]) <= 255 && Integer
							.parseInt(ipArray[1]) >= 0)
					&& (Integer.parseInt(ipArray[2]) <= 255 && Integer
							.parseInt(ipArray[2]) >= 0)
					&& (Integer.parseInt(ipArray[3]) <= 255 && Integer
							.parseInt(ipArray[3]) >= 0)) {
				return true;
			} else {
				return false;
			}			
		} else {
			return false;
		}
	}

}
